package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {


    @Autowired
    private AddressBookService addressBookService;

    /**\
     *新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> saveAddress(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        boolean save = addressBookService.save(addressBook);
        return save?R.success("新增地址成功！"):R.error("新增地址失败！");
    }

    /**
     * 设置默认地址
     */

    @PutMapping("/default")
    public R<String> setDefaultAddress(@RequestBody AddressBook addressBook){
        Long addressBookId = addressBook.getId();
        //首先将所有的地址都设置为0
       // update address_book set is_default = 0 where  user_id =  ?

        LambdaUpdateWrapper<AddressBook> up = new LambdaUpdateWrapper<>();
        up.set(AddressBook::getIsDefault,0);
        up.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        addressBookService.update(up);

        //再将当前的id，也会就是这个地址默认设置为1
        LambdaUpdateWrapper<AddressBook> up2 = new LambdaUpdateWrapper<>();
        up2.set(AddressBook::getIsDefault,1);
        up2.eq(AddressBook::getId,addressBookId);
        boolean update = addressBookService.update(up2);
        return update?R.success("设置默认地址成功！"):R.error("设置默认地址失败！");

    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return addressBook!=null?R.success(addressBook):R.error("查询失败!");
    }

    /**
     * 查询默认地址
     */
     @GetMapping("/default")
    public R<AddressBook> getDefaultAddress(){
         //先写个sql语句呗
         //select * from AddressBook where 默认=1;
         LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
         qw.eq(AddressBook::getIsDefault,1);
         qw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
         AddressBook addressBook = addressBookService.getOne(qw);
         if(addressBook==null){
             throw new CustomException("默认地址为空");
         }
         return R.success(addressBook);
     }

    /**
     * 查询指定用户的全部地址
     * @return
     */
     @GetMapping("/list")
    public R<List<AddressBook>> getAddress(){
         //select * from addressBook where uerId=
         LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
         qw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
         List<AddressBook> list = addressBookService.list(qw);
         return list!=null?R.success(list):R.error("查询失败！");
     }

     @DeleteMapping
    public R<String> deleteAddress(@RequestParam List<Long> ids){
         for (Long id : ids) {
             addressBookService.removeById(id);
         }
         return R.success("删除成功！");
     }

     @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
         boolean update = addressBookService.update(addressBook, null);
         return update?R.success("地址修改成功！"):R.error("地址修改失败！");
     }









}
